function enregistrerPortlets(event, ui)
{
	
	portlets_changed = false;
	
	var portlets_gauche = '';
	var portlets_droite = '';
	
	$('#accueil-gauche').find('input[name=portlet_id]').each(function() {
		portlets_gauche = portlets_gauche+$(this).val()+'|';
	});
	$('#accueil-droite').find('input[name=portlet_id]').each(function() {
		portlets_droite = portlets_droite+$(this).val()+'|';
	});
	
	cp.ajaxService({
		action:				'cp-accueil-updPortlets',
		portlets_gauche:	portlets_gauche,
		portlets_droite:	portlets_droite
	}, function(reponse) {}, function(reponse) {});
	
}

$(document).ready(function()
{
	
	cp.ajaxService({
		action:	'cp-accueil-portlets'
	}, function(reponse) {
		
		// Mise à jour
		var version = $('new_v', reponse).text();
		if(version != '')
		{
			$('div.accueil-portlet-version').append(
				'<div class="msg msgInfo">'+
					str_replace('%s', version, cp.msg.upd_disponible)+
					'<ul>'+
						'<li><a style="font-weight:bold;" href="index.php?module=update">'+cp.msg.upd+'</a></li>'+
						'<li><a target="_blank" href="http://www.clicpage.net/versions/'+version+'">'+cp.msg.upd_a_propos+'</a></li>'+
					'</ul>'+
				'</div>'
			);
		}
		
		// Actualités
		var html_actualites = '';
		$('actualites>element', reponse).each(function() {
			html_actualites = html_actualites+
				'<h4>'+$('titre', this).text()+'</h4>'+
				'<p class="actualite-contenu"><span class="actualite-date">'+$('date', this).text()+'</span> '+$('contenu', this).text()+
				' <a href="'+$('lien', this).text()+'" target="_blank">'+cp.msg.suite+'</a></p>';
		});
		
		if(html_actualites != '')
		{
			$('#actualites-conteneur').html(html_actualites);
			$('#portlet-actualites').show();
		}
		
	}, function(reponse) {});
	
	portlets_changed = false;
	
	$('.accueil-portlets>div').prepend('<a class="portlet-position" href="#"><img src="images/16/deplacer.png" alt="'+cp.msg.deplacer+'"/></a>')
		.mouseenter(function() {
			$(this).find('a.portlet-position').show();
		}).mouseleave(function() {
			$(this).find('a.portlet-position').hide();
		});
	
	$('.accueil-portlets').sortable({
		connectWith:	'.accueil-portlets',
		containment:	'window',
		placeholder: 	'ui-state-highlight',
		change:			function(event,ui) { portlets_changed = true; },
		stop:			enregistrerPortlets,
		handle:			'a.portlet-position'
	});
	
});